import { Dispatch } from "react";
import { PersonActions, PersonState, PsersonActionType } from "./usePersonReducer";

interface PersonHookState {
    onExit: ()=>void
    setAge: (age: number) => void;
    setGroup: (group: string[]) => void;
    setName: (name: string) => void;
    setLoading: (loading: boolean) => void;
}

export const usePersonContainer = (state: PersonState, dispatch: Dispatch<PersonActions>): PersonHookState => {
    const setAge = (age: number) => {
        console.info("state",state);
        dispatch({
            type: PsersonActionType.SET_AGE,
            value: age
        });
    };

     const setName = (name: string) => {
        dispatch({
            type: PsersonActionType.SET_NAME,
            value: name
        });
    };

    const setGroup = (group: string[]) => {
        dispatch({
            type: PsersonActionType.SET_GROUP,
            value: group
        });
    };

    const setLoading = (loading: boolean) => {
        dispatch({
            type: PsersonActionType.SET_LOADING,
            value: loading
        });
    };

    const onExit = () => {
        console.info('Exit');
    };

    return {
        onExit,
        setAge,
        setGroup,
        setName,
        setLoading
    };
};